package video.com.controller;


import com.Exception.GlobalException;
import com.responseData.R;
import com.responseData.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import video.com.entity.Tag;
import video.com.service.TagService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwj
 * @since 2024-03-05
 */
@RestController
@RequestMapping("/video/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    /**
     * 根据videoId 获取视频的标签
     * @param vid
     * @return
     */
    @GetMapping("/getTag/{vid}")
    public R getTagByVideoId(@PathVariable String vid){
        List<Tag> tagListByVid = new ArrayList<>();
        try{
            tagListByVid = tagService.getTagListByVid(vid);
        }catch (Exception e){
            if(e instanceof GlobalException){
                R r = ((GlobalException) e).getR();
                return r;
            }
            return ResponseData.error("异常错误");
        }

        return ResponseData.success(tagListByVid);
    }

    @GetMapping("/checkTag/{tagname}")
    public R checkTag(@PathVariable String tagname){
        Tag tag = new Tag();
        try{
            tag = tagService.tagInfo(tagname);
        }catch (Exception e){
            if(e instanceof GlobalException){
                R r = ((GlobalException) e).getR();
                return r;
            }
            return ResponseData.error("异常错误");
        }

        return ResponseData.success(tag);
    }
}

